<template>
	<view class="page">
		<CustomNav :isBack="true" :topBgColor="topBgColor" title="设置"></CustomNav>
		<view class="menu-block">
			<view @click="navigate('/pages/about/index')" class="menu-item">
				<image class="menu-icon" src="@/static/image/mine/guanyuwomen.png"></image>
				<view class="menu-name">关于我们</view>
				<view class="iconfont icon-a-gengduo1"></view>
			</view>
			<view @click="openWebView(privacyUrl)" class="menu-item">
				<image class="menu-icon" src="@/static/image/mine/privacy.png"></image>
				<view class="menu-name">隐私政策</view>
				<view class="iconfont icon-a-gengduo1"></view>
			</view>
			<view @click="openWebView(agreementUrl)" class="menu-item">
				<image class="menu-icon" src="@/static/image/mine/agreement.png"></image>
				<view class="menu-name">用户协议</view>
				<view class="iconfont icon-a-gengduo1"></view>
			</view>
			<view @click="navigate('/pages/mine/password')" class="menu-item">
				<image class="menu-icon" src="@/static/image/mine/password.png"></image>
				<view class="menu-name">修改密码</view>
				<view class="iconfont icon-a-gengduo1"></view>
			</view>
			<view @click="testVoice" class="menu-item">
				<image class="menu-icon" src="@/static/image/mine/guanyuwomen.png"></image>
				<view class="menu-name">测试语音播报</view>
				<view class="iconfont icon-a-gengduo1"></view>
			</view>
			<view @click="checkBackgroundPermission" class="menu-item">
				<image class="menu-icon" src="@/static/image/mine/guanyuwomen.png"></image>
				<view class="menu-name">允许播报权限</view>
				<view class="iconfont icon-a-gengduo1"></view>
			</view>
			<view class="menu-item" @click="deleteAccount">
				<image class="menu-icon" src="@/static/image/mine/zhuxiao.png">
				</image>
				<view class="menu-name">注销账号</view>
				<view class="iconfont icon-a-gengduo1"></view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				topBgColor: 'rgba(0,0,0,0)',
				privacyUrl: 'https://quansutong.quansuchong.com/privacy.html',
				agreementUrl: 'https://quansutong.quansuchong.com/user.html'
			}
		},
		methods: {
			checkBackgroundPermission() {
				// #ifdef APP-PLUS
				if (uni.getSystemInfoSync().platform === 'android') {
					// 获取手机品牌
					const brand = uni.getSystemInfoSync().brand.toLowerCase();
					
					uni.showModal({
						title: '提示',
						content: '为了确保语音播报功能正常运行，请允许应用在后台运行',
						confirmText: '去设置',
						success: (res) => {
							if (res.confirm) {
								// 根据不同品牌跳转到相应的设置页面
								const main = plus.android.runtimeMainActivity();
								const Intent = plus.android.importClass('android.content.Intent');
								const Settings = plus.android.importClass('android.provider.Settings');
								const intent = new Intent();
								
								// 统一使用系统应用详情页跳转
								try {
									intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
									intent.setData(plus.android.importClass("android.net.Uri").parse("package:" + main.getPackageName()));
									main.startActivity(intent);
								} catch (e) {
									uni.showToast({
										title: '跳转失败，请手动设置',
										icon: 'none'
									});
									console.error('跳转设置页面失败:', e);
								}
							}
						}
					});
				}
				// #endif
			},
			testVoice() {
				const app = getApp();
				if (app && app.testAudioPlay) {
					uni.showToast({
						title: '正在播放测试音频',
						icon: 'none',
						duration: 2000
					});
					app.testAudioPlay();
				} else {
					uni.showToast({
						title: '无法播放测试音频',
						icon: 'none'
					});
				}
			},
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			openWebView(url) {
				if (!url) {
					uni.showToast({
						title: '链接无效',
						icon: 'none'
					})
					return
				}
				
				const encodedUrl = encodeURIComponent(url)
				console.log('Opening webview with URL:', url)
				
				uni.navigateTo({
					url: '/pages/webview/index?url=' + encodedUrl,
					fail: (err) => {
						console.error('Failed to open webview:', err)
						uni.showToast({
							title: '打开页面失败',
							icon: 'none'
						})
					}
				})
			},
			deleteAccount() {
				uni.showModal({
					title: '提示',
					content: '确定要注销账号吗？注销后将无法恢复，请谨慎操作！',
					confirmColor: '#EA0000',
					success: (res) => {
						if (res.confirm) {
							this.$store.dispatch('user/logout')
							uni.reLaunch({
								url:'/pages/login/login'
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		overflow: hidden;
		height: 100%;
		background-color: #f5f5f5;
		
		.menu-block {
			width: $content-width;
			margin: 0 auto;
			margin-top: 30rpx;
			position: relative;

			.menu-item {
				width: 100%;
				background: #fff;
				border-radius: 8px;
				height: 130rpx;
				display: flex;
				align-items: center;
				padding: 30rpx;
				margin-bottom: 20rpx;

				.menu-icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 20rpx;
				}

				.menu-name {
					flex-grow: 1;
					font-size: 28rpx;
				}

				.iconfont {
					color: #767676;
					font-size: 28rpx;
				}
			}
		}
	}
</style> 